//
//  UserMangerDetailVIewController.m
//  YIJU2.0-HD
//
//  Created by 张金国 on 16/5/11.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "UserMangerDetailVIewController.h"
#import "Masonry.h"
#import "Macro.h"
#import "UserBaseInfoView.h"
#import "UserPurchWishView.h"
#import "UserAttentionHouseTypeView.h"
#import "CheckingRecordView.h"
#import "HouseTypeView.h"
#import "MagazineView.h"
#import "PushActivityView.h"
#import "UIColor+RandomColor.h"
#import "AddCheckingRecordView.h"

#define kMainViewTextColor [UIColor colorWithRed:0.851 green:0.8549 blue:0.8627 alpha:1.0]
#define kMainViewBtnBackgroundColor [UIColor colorWithRed:0.2157 green:0.2627 blue:0.3255 alpha:1.0]

@interface UserMangerDetailVIewController ()<UIScrollViewDelegate>

@property (nonatomic ,strong)UIView                     *mainView;//左边主view
@property (nonatomic ,strong)UIView                     *maskView;//左边放按钮的view
@property (nonatomic ,strong)UIScrollView               *mainScrollView;//右边滑动scroll
@property (nonatomic ,strong)UserBaseInfoView           *baseUserInfoView;//用户基本信息
@property (nonatomic ,strong)UserPurchWishView          *userPurchWishView;//用户购房意愿
@property (nonatomic ,strong)UserAttentionHouseTypeView *userAttentionHouseTypeView;//用户关注的房屋及户型
@property (nonatomic ,strong)CheckingRecordView         *checkingRecordView;//用户看房记录
@property (nonatomic ,strong)HouseTypeView              *houseTypeView;//推送户型
@property (nonatomic ,strong)MagazineView               *magazineView;//推送杂志
@property (nonatomic ,strong)PushActivityView           *pushActivityView;//推动活动
@property (nonatomic ,strong)AddCheckingRecordView      *addCheckingRecordView;//新增看房记录

@property (nonatomic, strong)NSArray                    *MainViewBtnTitleArray;//左边按钮title数组
@end

@implementation UserMangerDetailVIewController

#pragma mark - Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kDockBackgroundColor;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self MainViewBtnTitleArray];
    [self layoutUI];
    
    __weak __typeof__(self) weakSelf = self;

    self.checkingRecordView.block = ^{
        DDLogInfo(@"新增");
        
        [UIView animateWithDuration:0.4 animations:^{
               weakSelf.addCheckingRecordView.hidden = NO;
        }];
     
    };
    
    self.houseTypeView.block = ^{
        DDLogInfo(@"推送户型");
    };
    
    self.magazineView.block = ^{
        DDLogInfo(@"推送杂志");
    };
    
    self.pushActivityView.block = ^{
        DDLogInfo(@"推送活动");
    };
}
#pragma mark - private
- (void)layoutUI{
    [self.mainView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.equalTo(self.view);
        make.width.equalTo(@(SCREEN_WIDTH*0.087));
    }];
    
    [self.mainScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view.mas_top);
        make.height.equalTo(self.view).offset(-TweentyOffsetViewMargin);
        make.left.equalTo(self.mainView.mas_right);
        make.right.equalTo(self.view.mas_right);
    }];
    
    [self.baseUserInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.mainScrollView.mas_left);
        make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
    }];

    [self.userPurchWishView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.baseUserInfoView.mas_right).offset(1);
        make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
           }];

    [self.userAttentionHouseTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.userPurchWishView.mas_right).offset(1);
        make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
    }];

    [self.checkingRecordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.userAttentionHouseTypeView.mas_right).offset(1);
        make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
    }];
    
    self.addCheckingRecordView = [[AddCheckingRecordView alloc]init];
//    self.addCheckingRecordView.backgroundColor = [UIColor redColor];
    self.addCheckingRecordView.hidden = YES;
    [self.checkingRecordView addSubview:self.addCheckingRecordView];
    
    [self.addCheckingRecordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.checkingRecordView);
    }];
    

    [self.houseTypeView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.checkingRecordView.mas_right).offset(1);
         make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
    }];

    [self.magazineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.houseTypeView.mas_right).offset(1);
        make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
    }];

    [self.pushActivityView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainScrollView.mas_top).offset(TweentyOffsetViewMargin);
        make.height.equalTo(self.mainScrollView);
        make.left.equalTo(self.magazineView.mas_right).offset(1);
        make.width.equalTo(@(SCREEN_WIDTH/2.0f*0.914));
        make.right.equalTo(self.mainScrollView);
    }];

    
    
    [self layoutForMainView];
}

- (void)layoutForMainView {
    //头像
    UIImageView *imageView = [[UIImageView alloc]init];
    imageView.image = [UIImage imageNamed:@"HeadImage"];
    imageView.layer.cornerRadius = (_mainView.frame.size.width / 5) * 2;
    imageView.clipsToBounds = YES;
    [self.mainView addSubview:imageView];
    //昵称
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = @"机器猫";
    nameLabel.textColor = kMainViewTextColor;
    nameLabel.font = sTitle_Font;
    nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.mainView addSubview:nameLabel];
    //上次登录时间title
    UILabel *lastTime  = [[UILabel alloc]init];
    lastTime.text = @"上次登录";
    lastTime.textColor = kMainViewTextColor;
    lastTime.font = kTitleFont_11;
    lastTime.textAlignment = NSTextAlignmentCenter;
    [self.mainView addSubview:lastTime];
    //登录时间
    UILabel *timeLabel = [[UILabel alloc]init];
    timeLabel.textColor = kMainViewTextColor;
    timeLabel.text = @"2015.6.18 16:00";
    timeLabel.font = kTitleFont_11;
    timeLabel.textAlignment = NSTextAlignmentCenter;
    [self.mainView addSubview:timeLabel];
    
    //转重要客户按钮
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    button1.backgroundColor = kMainViewBtnBackgroundColor;
    [button1 setTitleColor:kMainViewTextColor forState:0];
    [button1 setTitle:@"转重要客户" forState:0];
    button1.layer.cornerRadius = 5.0f;
    button1.titleLabel.font = kTitleFont_11;
    button1.tag = 2000;
    [button1 addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:button1];
    //返回用户列表按钮
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeCustom];
    button2.backgroundColor = kMainViewBtnBackgroundColor;
    [button2 setTitleColor:kMainViewTextColor forState:0];
    [button2 setTitle:@"返回用户列表" forState:0];
    button2.layer.cornerRadius = 5.0f;
    button2.titleLabel.font = kTitleFont_11;
    button2.tag = 2001;
    [button2 addTarget:self action:@selector(buttonEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.mainView addSubview:button2];

    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.mainView.mas_top).offset(TweentyOffsetViewMargin*2);
        make.centerX.equalTo(self.mainView.mas_centerX);
        make.width.height.equalTo(self.mainView.mas_width).multipliedBy(4/5.0f);
    }];
    
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(imageView.mas_centerX);
        make.top.equalTo(imageView.mas_bottom).offset(TenOffsetViewMargin);
        make.width.equalTo(imageView.mas_width);
        make.height.equalTo(nameLabel.mas_width).multipliedBy(1/3.0f);
    }];
    
    [lastTime mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(nameLabel.mas_bottom).offset(TweentyOffsetViewMargin);
        make.centerX.equalTo(imageView.mas_centerX);
        make.width.equalTo(imageView.mas_width);
        make.height.equalTo(imageView.mas_height).multipliedBy(1/5.0f);
    }];
    
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(lastTime.mas_bottom).offset(FiveOffsetViewMargin);
        make.centerX.equalTo(imageView.mas_centerX);
        make.width.equalTo(imageView.mas_width);
        make.height.equalTo(imageView.mas_height).multipliedBy(1/5.0f);
    }];
    
    //放中间按钮的view
    [self.maskView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(timeLabel.mas_bottom).offset(TweentyOffsetViewMargin + TenOffsetViewMargin);
        make.left.equalTo(self.mainView.mas_left);
        make.width.equalTo(self.mainView.mas_width);
        make.height.equalTo(self.mainView.mas_height).multipliedBy(1.3/3.0f);
    }];
    
    [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mainView.mas_centerX);
        make.left.equalTo(self.mainView.mas_left).offset(FiveOffsetViewMargin);
        make.right.equalTo(self.mainView.mas_right).offset(-FiveOffsetViewMargin);
        make.height.equalTo(imageView.mas_height).multipliedBy(1/2.0f);
        make.bottom.equalTo(button2.mas_top).offset(-TenOffsetViewMargin);
    }];
    
    [button2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mainView.mas_centerX);
        make.left.equalTo(self.mainView.mas_left).offset(FiveOffsetViewMargin);
        make.right.equalTo(self.mainView.mas_right).offset(-FiveOffsetViewMargin);
        make.height.equalTo(imageView.mas_height).multipliedBy(1/2.0f);
        make.bottom.equalTo(self.mainView.mas_bottom).offset(-TweentyOffsetViewMargin*2);
    }];
    
    [self layOutTypeButton];
}

- (void)layOutTypeButton {
    UIButton *tempButton = nil;
    for (int i = 0; i < 7; i ++) {
        UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [leftButton setTitle:_MainViewBtnTitleArray[i] forState:0];
        [leftButton setBackgroundColor:[UIColor clearColor]];
        [leftButton setTitleColor:kMainViewTextColor forState:0];
        leftButton.titleLabel.font = sTitle_Font;
        leftButton.tag = 3000 + i;
        [leftButton addTarget:self action:@selector(buttonTypeEvent:) forControlEvents:UIControlEventTouchUpInside];
        [_maskView addSubview:leftButton];
        
        if (tempButton == nil) {
            [leftButton setBackgroundColor:kMainViewBtnBackgroundColor];
            [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(self.maskView.mas_top);
                make.left.equalTo(self.maskView.mas_left);
                make.width.equalTo(self.maskView.mas_width);
                make.height.equalTo(self.maskView.mas_height).multipliedBy(1/7.0f);
            }];
        } else {
            [leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(tempButton.mas_bottom);
                make.left.equalTo(self.maskView.mas_left);
                make.width.equalTo(self.maskView.mas_width);
                make.height.equalTo(self.maskView.mas_height).multipliedBy(1/7.0f);
            }];
        }
        tempButton = leftButton;
    }
}

#pragma mark - event response
//左边底部按钮事件
- (void)buttonEvent:(UIButton *)button {
    NSInteger btnTag = button.tag - 2000;
    switch (btnTag) {
        case 0://转重要客户
            
            break;
            
        case 1://返回用户列表
            [self dismissViewControllerAnimated:YES completion:nil];
            break;
            
        default:
            break;
    }
}

//左边中间按钮事件
- (void)buttonTypeEvent:(UIButton *)button {
    NSInteger btnTag = button.tag - 3000;
    for (int i = 3000; i < 3007; i ++) {
        UIButton *buttons = [self.maskView viewWithTag:i];
        [buttons setBackgroundColor:[UIColor clearColor]];
    }
    [button setBackgroundColor:kMainViewBtnBackgroundColor];
    
    btnTag == 6 ? [self scrollToPoint:5] : [self scrollToPoint:btnTag];
}

- (void)scrollToPoint:(NSInteger)tempTag {
    NSInteger currentIndex = ((self.mainScrollView.contentOffset.x) * 2 / self.mainScrollView.bounds.size.width);
    currentIndex == 6 ? currentIndex = 5 : currentIndex;
    NSInteger animateTime = ABS(tempTag - currentIndex);
    [UIView animateWithDuration:animateTime * 0.2 animations:^{
        [self.mainScrollView setContentOffset:CGPointMake(tempTag*SCREEN_WIDTH/2.0f*0.914, 0) animated:NO];
    }];
    
}

#pragma mark - UIScrollViewDelegate 
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger currentIndex = ((scrollView.contentOffset.x) * 2 / scrollView.bounds.size.width);
    for (int i = 3000; i < 3007; i ++) {
        UIButton *buttons = [self.maskView viewWithTag:i];
    
      [buttons setBackgroundColor:[UIColor clearColor]];
    }
    UIButton *button = [self.maskView viewWithTag:currentIndex + 3000];
    [button setBackgroundColor:kMainViewBtnBackgroundColor];
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    
}


#pragma mark - getter
- (UIView *)mainView {
    if (!_mainView) {
        _mainView = [[UIView alloc]init];
        _mainView.backgroundColor = kDockBackgroundColor;
        [self.view addSubview:_mainView];
    }
    return _mainView;
}

- (UIView *)maskView {
    if (!_maskView) {
        _maskView = [[UIView alloc]init];
        _maskView.userInteractionEnabled = YES;
        _maskView.backgroundColor = [UIColor clearColor];
        [self.mainView addSubview:_maskView];
    }
    return _maskView;
}

- (UIScrollView *)mainScrollView {
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc]init];
        _mainScrollView.contentSize = CGSizeMake((SCREEN_WIDTH/2.0f*0.914)*7,_mainScrollView.contentSize.height);
        _mainScrollView.backgroundColor = kDockBackgroundColor;
        _mainScrollView.userInteractionEnabled = YES;
        _mainScrollView.scrollEnabled = YES;
        _mainScrollView.showsHorizontalScrollIndicator = YES;
        _mainScrollView.bounces = NO;//禁止边缘滑动
        _mainScrollView.delegate = self;
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}


- (UserBaseInfoView *)baseUserInfoView {
    if (!_baseUserInfoView) {
        _baseUserInfoView = [[UserBaseInfoView alloc]init];
        [self.mainScrollView addSubview:_baseUserInfoView];
        _baseUserInfoView.isShow = YES;
        _baseUserInfoView.labeText = @"用户基本信息";
        _baseUserInfoView.imageName = @"Profile";
        
    }
    return _baseUserInfoView;
}

- (UserPurchWishView *)userPurchWishView {
    if (!_userPurchWishView) {
        _userPurchWishView = [[UserPurchWishView alloc]init];
        [self.mainScrollView addSubview:_userPurchWishView];
        _userPurchWishView.isShow = YES;
        _userPurchWishView.labeText = @"用户购房意愿";
        _userPurchWishView.imageName = @"check";
        
    }
    return _userPurchWishView;
}


- (UserAttentionHouseTypeView *)userAttentionHouseTypeView {
    if (!_userAttentionHouseTypeView) {
        _userAttentionHouseTypeView = [[UserAttentionHouseTypeView alloc]init];
        _userAttentionHouseTypeView.labeText = @"用户关注房屋及户型";
        _userAttentionHouseTypeView.imageName = @"store";
        _userAttentionHouseTypeView.isShow = YES;
        
        [self.mainScrollView addSubview:_userAttentionHouseTypeView];
    }
    return _userAttentionHouseTypeView;
}

- (CheckingRecordView *)checkingRecordView {
    if (!_checkingRecordView) {
        _checkingRecordView = [[CheckingRecordView alloc]init];
        _checkingRecordView.buttonText = @"新增";
        _checkingRecordView.labeText = @"用户的看房记录";
        _checkingRecordView.imageName = @"pencil";
        
        [self.mainScrollView addSubview:_checkingRecordView];
    }
    return _checkingRecordView;
}


- (HouseTypeView *)houseTypeView {
    if (!_houseTypeView) {
        _houseTypeView = [[HouseTypeView alloc]init];
        _houseTypeView.buttonText = @"搜索";
        _houseTypeView.labeText = @"推送户型";
        _houseTypeView.imageName = @"push";
        _houseTypeView.items = 10;
        [self.mainScrollView addSubview:_houseTypeView];
    }
    return _houseTypeView;
}

- (MagazineView *)magazineView {
    if (!_magazineView) {
        _magazineView = [[MagazineView alloc]init];
        _magazineView.buttonText = @"搜索";
        _magazineView.labeText = @"推送杂志";
        _magazineView.imageName = @"push";
        _magazineView.items = 15;
        [self.mainScrollView addSubview:_magazineView];
    }
    return _magazineView;
}

- (PushActivityView *)pushActivityView {
    if (!_pushActivityView) {
        _pushActivityView = [[PushActivityView alloc]init];
        _pushActivityView.buttonText = @"搜索";
        _pushActivityView.labeText = @"推送活动";
        _pushActivityView.imageName = @"push";
        [self.mainScrollView addSubview:_pushActivityView];
    }
    return _pushActivityView;
}

- (NSArray *)MainViewBtnTitleArray {
    if (!_MainViewBtnTitleArray) {
        _MainViewBtnTitleArray = @[@"基本资料",@"买房意愿",@"用户关注",@"看房记录",@"推送户型",@"推送杂志",@"推送活动"];
    }
    return _MainViewBtnTitleArray;
}
@end
